package pers.youqu.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.youqu.item.dto.CategoryDTO;
import pers.youqu.item.mapper.CategoryMapper;
import pers.youqu.item.po.Category;
import pers.youqu.item.po.CategoryBrand;
import pers.youqu.item.service.CategoryBrandService;
import pers.youqu.item.service.CategoryService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/4 11:41
 * @Description TODO
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public CategoryDTO findCategoryById(Long id) {
        return new CategoryDTO(this.getById(id));
    }

    @Override
    public List<CategoryDTO> findCategoryByListId(List<Long> ids) {
        return CategoryDTO.convertEntityList(this.listByIds(ids));
    }

    @Override
    public List<CategoryDTO> findCategoryByBrandId(Long id) {

        List<CategoryBrand> categoryBrands = categoryBrandService.list(new QueryWrapper<CategoryBrand>().eq("brand_id", id));
        List<Long> categoryIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        return CategoryDTO.convertEntityList(this.listByIds(categoryIds));
    }

    @Override
    public List<CategoryDTO> findCategoryByParentId(Long pid) {
        return CategoryDTO.convertEntityList(this.query().eq("parent_id", pid).list());
    }
}
